﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ext.Net;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Linq;
using System.IO;

namespace Enterprise.Web
{
    public class BaseUserControl:System.Web.UI.UserControl
    {

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Page.IsPostBack)
            {
                this.InitializeControls();
            }
        }

        public virtual void InitializeControls()
        {
            
        }

        public virtual void BindData() { }

        public virtual void LoadData() { }

        public virtual void SaveData() { }

        public virtual void SetUpUIForAddNewDialog()
        {
        
        }

        public virtual void SetUpUIForEditDialog()
        {
        
        }

        public virtual void SetUpUIForViewDialog()
        {

        }

        #region "Event"
        protected virtual void OnClick_Clear(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnClick_Edit(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnClick_Create(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void OnClick_Copy(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        
        public virtual void OnClick_Delete(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        [DirectMethod]
        public virtual void DoDelete()
        {
            throw new NotImplementedException();
        }

        protected virtual void OnClick_Save(object sender, DirectEventArgs e)
        {
            throw new NotImplementedException();
        }

        protected virtual void Clear()
        { 
            
        }
        #endregion

        #region properties
        // ID cua grid
        private int _gridID = 0;
        public int GridID
        {
            get
            {
                return _gridID;
            }
            set
            {
                _gridID = value;
            }
        }
        #endregion

        protected virtual void StoMainSource_Submit(object sender, StoreSubmitDataEventArgs e)
        {

        }

        protected void Export(string format, string strDownloadFileName, StoreSubmitDataEventArgs e)
        {
            XmlNode xml = e.Xml;

            this.Response.Clear();

            switch (format)
            {
                case "xml":
                    string strXml = xml.OuterXml;
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xml");
                    this.Response.AddHeader("Content-Length", strXml.Length.ToString());
                    this.Response.ContentType = "application/xml";
                    this.Response.Write(strXml);
                    break;

                case "xls":
                    this.Response.ContentType = "application/vnd.ms-excel";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.xls");
                    XslCompiledTransform xtExcel = new XslCompiledTransform();
                    xtExcel.Load(Server.MapPath("Template/Excel.xsl"));
                    xtExcel.Transform(xml, null, Response.OutputStream);

                    break;

                case "csv":
                    this.Response.ContentType = "application/octet-stream";
                    this.Response.AddHeader("Content-Disposition", "attachment; filename=submittedData.csv");
                    XslCompiledTransform xtCsv = new XslCompiledTransform();
                    xtCsv.Load(Server.MapPath("Template/Csv.xsl"));
                    xtCsv.Transform(xml, null, Response.OutputStream);

                    break;
            }
            this.Response.End();
        }

        public List<XCommandColoumn> GetGridCommandWithUser(string username)
        {
            List<XCommandColoumn> lstCommand = new List<XCommandColoumn>();
            string xmlFile = Server.MapPath(Enterprise.CoreGlobal.FILEPATH_GRIDCOMMAND_XML);
            string xmlContent = new StreamReader(xmlFile).ReadToEnd();
            XDocument xdoc = XDocument.Parse(xmlContent);
            if (xdoc != null)
            {
                var newdataset =
                    (from grid in xdoc.Descendants("Grid")
                     where grid.Attribute("ID").Value.Equals(_gridID.ToString())
                     select new
                     {
                         id = grid.Attribute("ID"),
                         command = (from commands in grid.Elements("Commands").Elements("CommandID")
                                    select new List<string>()
                                      {
                                          commands.Value
                                      }).ToList()
                     }).SingleOrDefault();

                List<string> commandList = new List<string>();

                foreach (var item in newdataset.command)
                {
                    foreach (var item1 in item)
                    {
                        commandList.Add(item1.ToString());
                    }
                }


                lstCommand = (from commands in xdoc.Descendants("Command")
                     where commandList.Contains(commands.Element("ID").Value.ToString())
                     select new XCommandColoumn
                     {
                         ID = commands.Element("ID"),
                         Name = commands.Element("Name"),
                         Type = commands.Element("Type"),
                         Icon = commands.Element("Icon"),
                     }).ToList();

            }
            return lstCommand;
        }

    }

    public class XCommandColoumn
    {
        public System.Xml.Linq.XElement ID;
        public System.Xml.Linq.XElement Name;
        public System.Xml.Linq.XElement Type;
        public System.Xml.Linq.XElement Icon;
    }

    public enum DialogType
    { 
        
    }
}
